package com.rjwh.dingdong.client.activity.drawbook;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.image.ImageListBrowseActivity;
import com.rjwh.dingdong.client.activity.loginregist.ThirdPartyLoginActivity;
import com.rjwh.dingdong.client.activity.webbrowser.OaGameActivity;
import com.rjwh.dingdong.client.activity.webbrowser.WebActivity;
import com.rjwh.dingdong.client.adapter.DrawBookDetailCommentAdapter;
import com.rjwh.dingdong.client.adapter.DrawBookDetailModuleBookListAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.DownloadInfo;
import com.rjwh.dingdong.client.bean.localbean.DrawBookCommentData;
import com.rjwh.dingdong.client.bean.localbean.DrawBookDetailData;
import com.rjwh.dingdong.client.bean.localbean.DrawBookDetailExtraData;
import com.rjwh.dingdong.client.bean.localbean.DrawBookDetailModuleData;
import com.rjwh.dingdong.client.bean.localbean.DrawBookDetialModuleBookListData;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.download.DownloadManager;
import com.rjwh.dingdong.client.mvp.presenter.impl.DrawBookDetailPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookDetailView;
import com.rjwh.dingdong.client.player.IPlayback;
import com.rjwh.dingdong.client.service.PlaybackService;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.RxBus;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.view.ExpandableTextView;
import com.rjwh.dingdong.client.view.NoScrollListView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;

/**
 * @author Kay
 * @ClassName DrawBookDetailActivity
 * @Description 绘本详情
 * @date 2016-6-30
 * @history 2016-6-30 author: description:
 */
public class DrawBookDetailActivity extends BaseActivity implements DrawBookDetailView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.loading_layout)
    RelativeLayout loadingLayout;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.draw_book_detail_book_img)
    ImageView drawBookDetailBookImg;
    @BindView(R.id.draw_book_detail_book_title_tv)
    TextView drawBookDetailBookTitleTv;
    @BindView(R.id.draw_book_detail_book_content_tv)
    ExpandableTextView drawBookDetailBookContentTv;
    @BindView(R.id.draw_book_detail_btn)
    Button drawBookDetailBtn;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.draw_book_detail_comment_list_lv)
    NoScrollListView drawBookDetailBookCommentListLv;
    @BindView(R.id.draw_book_detail_bottom_root_rl)
    RelativeLayout drawBookDetailBottomRootRl;
    @BindView(R.id.draw_book_detail_comment_title_times_tv)
    TextView drawBookDetailCommentTitleTimesTv;
    @BindView(R.id.draw_book_detail_age_tv)
    TextView drawBookDetailAgeTv;
    @BindView(R.id.draw_book_detail_type_tv)
    TextView drawBookDetailTypeTv;
    @BindView(R.id.draw_book_detail_read_times_tv)
    TextView drawBookDetailReadTimesTv;
    @BindView(R.id.draw_book_detail_comment_all_btn)
    TextView drawBookDetailCommentAllBtn;
    @BindView(R.id.draw_book_detail_module_text)
    TextView drawBookDetailModuleText;
    @BindView(R.id.draw_book_detail_module_more_arrow)
    TextView drawBookDetailModuleMoreArrow;
    @BindView(R.id.draw_book_detail_module_list)
    NoScrollListView drawBookDetailModuleList;
    @BindView(R.id.draw_book_detail_book_pic_tv)
    TextView drawBookDetailBookPicTv;
    @BindView(R.id.draw_book_detail_book_video_tv)
    TextView drawBookDetailBookVideoTv;
    @BindView(R.id.draw_book_detail_drawbook_preview_root)
    RelativeLayout drawBookDetailDrawbookPreviewRoot;
    @BindView(R.id.draw_book_detail_divide_view)
    View drawBookDetailDivideView;
    @BindView(R.id.draw_book_detail_module_root)
    RelativeLayout drawBookDetailModuleRoot;

    private Observable<DownloadInfo> observable;
    private int bookid;
    private DrawBookDetailData drawbook;
    private DrawBookDetailPresenter presenter;
    private DownloadInfo downloadInfo;
    private Observable<DownloadInfo> observableChange;
    private Observable<DownloadInfo> observableProg;
    private DrawBookDetailCommentAdapter commentAdapter;
    private MenuItem stepMenu;
    private TextView stepTip;
    private String gameUpdateVersion;
    private IPlayback mPlayer;
    private DrawBookDetailModuleBookListAdapter bookListAdapter;
    private boolean isStepDialogCanBeShowed = false;

    public static void startActivity(Context context, String bookId) {
        Intent intent = new Intent(context, DrawBookDetailActivity.class);
        intent.putExtra("bookid", bookId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_book_detail_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        startObserver();
        presenter = new DrawBookDetailPresenter(this, this);
        presenter.subscribe();
        doGetDrawBookDetail();
        initDownLoad();
    }

    private void initView() {
        initToolbarHelper();
        commentAdapter = new DrawBookDetailCommentAdapter(this, true);
        drawBookDetailBookCommentListLv.setFocusable(false);// 解决进入界面不定位到顶部
        drawBookDetailModuleList.setFocusable(false);// 解决进入界面不定位到顶部
        drawBookDetailBookCommentListLv.setAdapter(commentAdapter);
        drawBookDetailBookCommentListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (MyApplication.spUtil.getBoolPreferenceByParamName(DrawBookDetailActivity.this,
                        LocalConstant.SP_IS_LOGINED)) {
                    DrawBookDetailCommentListActivity.startActivity(DrawBookDetailActivity.this, bookid);
                } else {
                    ThirdPartyLoginActivity.startActivity(DrawBookDetailActivity.this);
                }
            }
        });

        bookListAdapter = new DrawBookDetailModuleBookListAdapter(this);
        drawBookDetailModuleList.setAdapter(bookListAdapter);
        drawBookDetailModuleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DrawBookDetailModuleBookListAdapter adapter = (DrawBookDetailModuleBookListAdapter) parent.getAdapter();
                DrawBookDetialModuleBookListData item = adapter.getItem(position);
                String actionurl = item.getActionurl();
                if (!AsStrUtils.isEmpty(actionurl)) {
                    UrlUtil.handelUrl(DrawBookDetailActivity.this, actionurl, true);
                }
            }
        });
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            bookid = Integer.valueOf(intent.getStringExtra("bookid"));
        }
    }

    private void doGetDrawBookDetail() {
        presenter.doGetDrawBookDetail("" + bookid);
    }

    private void initDownLoad() {
        if (presenter.isGameNotNull(bookid)) {
            if (presenter.isGameUpdateIng(bookid)) {
                LogUtil.e("DrawBookDetailActivity", "isUpdateing");
                if (downloadInfo != null) {
                    refreshState(downloadInfo);
                } else {
                    downloadInfo = DownloadManager.getInstance().getTaskByUrl(bookid);
                    refreshState(downloadInfo);
                }
            } else {
                initViewState(3);
            }
        } else {
            downloadInfo = DownloadManager.getInstance().getTaskByUrl(bookid);
            if (downloadInfo != null) {
                refreshState(downloadInfo);
            } else {
                initViewState(1);
            }
        }
    }

    private void startObserver() {
        observable = RxBus.get().register(LocalConstant.RX__POST_DOWNLOAD_STATE_UNZIP, DownloadInfo.class);
        observableChange = RxBus.get().register(LocalConstant.RX__POST_DOWNLOAD_STATE_CHANGE, DownloadInfo.class);
        observableProg = RxBus.get().register(LocalConstant.RX__POST_DOWNLOAD_STATE_PROGRES, DownloadInfo.class);

        Subscriber subscriber = new Subscriber<DownloadInfo>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(DownloadInfo localGmaeInfo) {
                if (localGmaeInfo.getGameId() == bookid) {
                    if (localGmaeInfo.getDownloadState() == DownloadManager.STATE_UNZIP_SUCESS) {
                        drawBookDetailBtn.setText(R.string.start_read);
                    } else {
                        refreshState(localGmaeInfo);
                    }
                }
            }
        };
        Subscriber subscriberChange = new Subscriber<DownloadInfo>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(DownloadInfo localGmaeInfo) {
                if (localGmaeInfo.getGameId() == bookid) {
                    refreshState(localGmaeInfo);
                }
            }
        };
        Subscriber subscriberProg = new Subscriber<DownloadInfo>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(DownloadInfo localGmaeInfo) {
                if (localGmaeInfo.getGameId() == bookid) {
                    progressBar.setProgress((int) localGmaeInfo.getProgress());
                }
            }
        };
        addSubscription(observable, subscriber);
        addSubscription(observableChange, subscriberChange);
        addSubscription(observableProg, subscriberProg);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("");
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu_drawbook_detial, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        stepMenu = menu.findItem(R.id.menu_drawbook_detial_step);
        stepTip = (TextView) stepMenu.getActionView().findViewById(R.id.menu_drawbook_detial_step_tip);
        boolean isforcedShow = MyApplication.spUtil.getBoolPreferenceByParamName(MyApplication.app, LocalConstant.SP_DRAWBOOK_STEP_FORCED_SHOW, true);
        if (isforcedShow) {
            stepTip.setVisibility(View.VISIBLE);
        } else {
            stepTip.setVisibility(View.GONE);
        }
        stepMenu.getActionView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionsItemSelected(stepMenu);
            }
        });
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_drawbook_detial_step:
                stepTip.setVisibility(View.GONE);
                OaGameActivity.startActivity(this, LocalConstant.DRAW_BOOK_STEP);
                MyApplication.spUtil.setBoolPreference(MyApplication.app, LocalConstant.SP_DRAWBOOK_STEP_FORCED_SHOW, false);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.draw_book_detail_btn, R.id.draw_book_detail_comment_tab_input_btn, R.id.draw_book_detail_comment_all_btn})
    public void onClick(View view) {
        boolean isLogined = MyApplication.spUtil.getBoolPreferenceByParamName(this,
                LocalConstant.SP_IS_LOGINED);
        if (isLogined) {
            switch (view.getId()) {
                case R.id.draw_book_detail_btn:

                    List<LocalGmaeInfo> localGmaeInfoList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "pid=" + bookid + " and type=1");

                    if (!localGmaeInfoList.isEmpty()) {
                        if (presenter.isGameUpdateIng(bookid)) {
                            doDownLoad();
                        } else {
                            //启动绘本
                            presenter.startGame(bookid, mPlayer);
                        }
                    } else {
                        if (isStepDialogCanBeShowed) {
                            //弹框提示进入步骤说明
                            boolean isforcedShow = MyApplication.spUtil.getBoolPreferenceByParamName(MyApplication.app, LocalConstant.SP_DRAWBOOK_STEP_FORCED_SHOW, true);
                            LogUtil.e("DrawBookFragment", "isforcedShow:" + isforcedShow);
                            if (isforcedShow) {
                                //如果没进入过步骤说明就弹框
                                showStepDialog();
                                isStepDialogCanBeShowed = false;
                            }
                        }
                        doDownLoad();
                    }
                    break;
                case R.id.draw_book_detail_comment_tab_input_btn:
                    Intent intent = new Intent(DrawBookDetailActivity.this, DrawBookDetailCommentInputActivity.class);
                    intent.putExtra("bookid", bookid);
                    intent.putExtra("tag", 2);
                    startActivity(intent);
                    break;
                case R.id.draw_book_detail_comment_all_btn:
                    DrawBookDetailCommentListActivity.startActivity(DrawBookDetailActivity.this, bookid);
                    break;

                default:
                    break;
            }
        } else {
            ThirdPartyLoginActivity.startActivity(this);
        }

    }

    /**
     * 更新弹框
     */
    private void showUpdateDialog(final LocalGmaeInfo localGmaeInfo) {
        new AlertDialog.Builder(this)
                .setTitle("当前版本已经升级，赶紧更新以便获得更好的体验吧~")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        doUpdate(localGmaeInfo);
                    }
                })
                .show();
    }

    //更新
    private void doUpdate(LocalGmaeInfo mData) {
        //先删除旧版本
        MyApplication.db.delete(mData);

        doDownLoad();

        mData.setIsUpdate(2);
        MyApplication.db.update(mData);
    }

    @Override
    public void supportFinishAfterTransition() {
        super.supportFinishAfterTransition();
    }

    // 下载
    private void doDownLoad() {
        presenter.doDownload(downloadInfo, drawbook, bookid);
    }

    private void showStepDialog() {
        new AlertDialog.Builder(this)
                .setTitle(getString(R.string.game_step_dialog_tip))
                .setNegativeButton(getString(R.string.user_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(getString(R.string.user_confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        stepTip.setVisibility(View.GONE);
                        MyApplication.spUtil.setBoolPreference(MyApplication.app, LocalConstant.SP_DRAWBOOK_STEP_FORCED_SHOW, false);
                        OaGameActivity.startActivity(DrawBookDetailActivity.this, LocalConstant.DRAW_BOOK_STEP);
                    }
                })
                .show();
    }

    public void initViewState(int type) {
        if (type == 3) {
            drawBookDetailBtn.setText(R.string.start_read);
        } else if (type == 1) {
            drawBookDetailBtn.setText(R.string.start_download);
            isStepDialogCanBeShowed = true;
        } else if (type == 2) {
            drawBookDetailBtn.setText(R.string.start_update);
        }
    }

    private void refreshState(DownloadInfo info) {

        downloadInfo = info;
        switch (info.getDownloadState()) {
            case DownloadManager.STATE_NONE:
                drawBookDetailBtn.setText(R.string.start_download);
                break;
            case DownloadManager.STATE_PAUSED:
                drawBookDetailBtn.setText(R.string.download_pause);
                drawBookDetailBtn.setBackgroundResource(R.drawable.transparent_btn_selector);
                progressBar.setProgress((int) info.getProgress());
                break;
            case DownloadManager.STATE_ERROR:
                drawBookDetailBtn.setText(R.string.download_fail);
                break;
            case DownloadManager.STATE_WAITING:
                drawBookDetailBtn.setText(R.string.download_wait);
                break;
            case DownloadManager.STATE_DOWNLOADING:
                drawBookDetailBtn.setText(R.string.download_downloading);
                drawBookDetailBtn.setBackgroundResource(R.drawable.transparent_btn_selector);
                progressBar.setProgress((int) info.getProgress());
                break;
            case DownloadManager.STATE_DOWNLOADED:
                drawBookDetailBtn.setText(R.string.download_finish);
                break;
            case DownloadManager.STATE_UNZIP:
                drawBookDetailBtn.setText(R.string.download_unziping);
                break;
            case DownloadManager.STATE_UNZIP_SUCESS:
                drawBookDetailBtn.setText(R.string.start_read);
                break;
            default:
                break;
        }
    }

    private void stopObserver() {
        RxBus.get().unregister(LocalConstant.RX__POST_DOWNLOAD_STATE_UNZIP, observable);
        RxBus.get().unregister(LocalConstant.RX__POST_DOWNLOAD_STATE_CHANGE, observableChange);
        RxBus.get().unregister(LocalConstant.RX__POST_DOWNLOAD_STATE_PROGRES, observableProg);
    }

    @Override
    public void showLoadingView() {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideLoadingView() {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showErrorView() {
        if (errorLayout != null) {
            errorLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideErrorView() {
        if (errorLayout != null) {
            errorLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void upDateView(DrawBookDetailData drawbook, DrawBookDetailExtraData drawbookextra) {
        this.drawbook = drawbook;
        String bookdescription = drawbook.getBookdescription();
        String bookname = drawbook.getBookname();
        String coverurl = drawbook.getCoverurl();
        String agestage = drawbook.getAgestage();
        String read = drawbook.getRead();
        String reply = drawbook.getReply();
        String typetitle = drawbook.getTypetitle();
        String version = drawbook.getVersion();

        final List<String> albumphotolist = drawbookextra.getAlbumphotolist();
        final String videourl = drawbookextra.getVideourl();

        if (coverurl != null) {
            Glide.with(this).load(coverurl).dontAnimate().placeholder(R.drawable.icon_drawbook_pic_holder).error(R.drawable.icon_drawbook_pic_holder).diskCacheStrategy(DiskCacheStrategy.ALL).into(drawBookDetailBookImg);
        }
        if (!AsStrUtils.isEmpty(bookname)) {
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(bookname);
            }
            drawBookDetailBookTitleTv.setText(bookname);
        }
        if (!AsStrUtils.isEmpty(bookdescription)) {
            drawBookDetailBookContentTv.setText(bookdescription);
        }
        if (!AsStrUtils.isEmpty(agestage)) {
            drawBookDetailAgeTv.setText(String.format(this.getString(R.string.drawbook_agestage_format), agestage));
        }
        if (AsStrUtils.isEmpty(read) || read.equals("0")) {
            drawBookDetailReadTimesTv.setVisibility(View.GONE);
        } else {
            drawBookDetailReadTimesTv.setVisibility(View.VISIBLE);
            drawBookDetailReadTimesTv.setText(String.format(this.getString(R.string.drawbook_read_format), read));
        }

        if (AsStrUtils.isEmpty(reply) || reply.equals("0")) {
            drawBookDetailCommentTitleTimesTv.setVisibility(View.GONE);
        } else {
            drawBookDetailCommentTitleTimesTv.setVisibility(View.VISIBLE);
            drawBookDetailCommentTitleTimesTv.setText(reply);
        }
        if (!AsStrUtils.isEmpty(typetitle)) {
            drawBookDetailTypeTv.setText(String.format(this.getString(R.string.drawbook_type_format), typetitle));
        }
        if (!AsStrUtils.isEmpty(version)) {
            this.gameUpdateVersion = version;
        }
        if (albumphotolist != null && !albumphotolist.isEmpty()) {
            drawBookDetailBookPicTv.setVisibility(View.VISIBLE);
            drawBookDetailBookPicTv.setText(String.format(this.getString(R.string.drawbook_pic_count_format), String.valueOf(albumphotolist.size())));
            drawBookDetailBookImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentBrowse = new Intent(DrawBookDetailActivity.this, ImageListBrowseActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) albumphotolist);
                    intentBrowse.putExtras(bundle);
                    intentBrowse.putExtra("position", 0);
                    startActivity(intentBrowse);
                }
            });
        } else {
            drawBookDetailBookPicTv.setVisibility(View.GONE);
        }
        if (!AsStrUtils.isEmpty(videourl)) {
            drawBookDetailDrawbookPreviewRoot.setVisibility(View.VISIBLE);
            drawBookDetailDrawbookPreviewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String webUrl = UrlUtil.getWebUrl(DrawBookDetailActivity.this, videourl);
                    WebActivity.startActivity(DrawBookDetailActivity.this, webUrl);
                }
            });
        } else {
            drawBookDetailDrawbookPreviewRoot.setVisibility(View.GONE);
        }

        checkUpdate();
    }

    private void checkUpdate() {
        List<LocalGmaeInfo> localGmaeInfoList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "pid=" + bookid + " and type=1");

        if (!localGmaeInfoList.isEmpty()) {
            if (presenter.isGameUpdate(this.gameUpdateVersion, bookid)) {
                if (presenter.isGameUpdateIng(bookid)) {
                    if (downloadInfo != null) {
                        refreshState(downloadInfo);
                    }
                } else {
                    LocalGmaeInfo localGmaeInfo = localGmaeInfoList.get(0);
                    showUpdateDialog(localGmaeInfo);
                }
            }
        }

    }

    @Override
    public void setDownLoadBtnShow(boolean isShow) {
        if (isShow) {
            drawBookDetailBottomRootRl.setVisibility(View.VISIBLE);
        } else {
            drawBookDetailBottomRootRl.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadCommentData(List<DrawBookCommentData> drawbookcommentlist) {
        commentAdapter.clearAll();
        commentAdapter.appendToList(drawbookcommentlist);
        if (drawbookcommentlist.isEmpty()) {
            drawBookDetailCommentAllBtn.setVisibility(View.GONE);
        } else {
            drawBookDetailCommentAllBtn.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void loadModuleData(DrawBookDetailModuleData module) {
        String modulename = module.getModulename();
        final String actionurl = module.getActionurl();
        List<DrawBookDetialModuleBookListData> booklist = module.getBooklist();

        if (!AsStrUtils.isEmpty(modulename)) {
            drawBookDetailModuleText.setText(modulename);
        }

        if (!AsStrUtils.isEmpty(actionurl)) {
            drawBookDetailModuleMoreArrow.setVisibility(View.VISIBLE);
            drawBookDetailModuleMoreArrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UrlUtil.handelUrl(DrawBookDetailActivity.this, actionurl, true);
                }
            });
        } else {
            drawBookDetailModuleMoreArrow.setVisibility(View.GONE);
        }

        if (booklist != null && !booklist.isEmpty()) {
            drawBookDetailDivideView.setVisibility(View.VISIBLE);
            drawBookDetailModuleRoot.setVisibility(View.VISIBLE);
            bookListAdapter.clearAll();
            bookListAdapter.appendToList(booklist);
        }else {
            drawBookDetailDivideView.setVisibility(View.GONE);
            drawBookDetailModuleRoot.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPlaybackServiceBound(PlaybackService service) {
        mPlayer = service;
    }

    @Override
    public void onPlaybackServiceUnbound() {
        mPlayer = null;
    }

    @OnClick(R.id.btn_refresh)
    public void onClick() {
        doGetDrawBookDetail();
    }

    @Override
    protected void onDestroy() {
        stopObserver();
        presenter.detachView();
        super.onDestroy();
    }
}
